package org.beals.thymetracker;

import org.beals.thymetracker.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.ContextMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.io.IOException;
//import java.lang.*;



@SuppressWarnings("unused")
public class ThymetrackerActivity extends Activity {
    /** Called when the activity is first created. */
//    @Override
    
    public long elapsedTime=0;
    public String currentTime="Free";
    public String theClient="";
    public String theActivity="";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Spinner spinner = (Spinner) findViewById(R.id.spiClient);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.client_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
  

        final Button butStart = (Button) findViewById(R.id.butStart);
        final Button butStop = (Button) findViewById(R.id.butStop);
        final Chronometer chrono;
        long startTime=SystemClock.elapsedRealtime();
        chrono = (Chronometer)findViewById(R.id.chronometer1);
        final EditText narrative = (EditText) findViewById(R.id.textView1);
        

        butStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Toast.makeText(getApplicationContext(), "The packageName is " +
                        getPackageName(), Toast.LENGTH_LONG).show();
                butStart.setEnabled(false);
                butStop.setEnabled(true);
                chrono.setBase(SystemClock.elapsedRealtime());
                chrono.start();
            }
        });

        butStop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	butStart.setEnabled(true);
            	butStop.setEnabled(false);
            	chrono.stop();
            	chrono.setText(currentTime);
            	theActivity = narrative.getText().toString();
            	try {
            		DataBaseHelper.enterRecord(theClient, currentTime, theActivity);
            	} catch (IOException e) {
            		Toast.makeText(getApplicationContext(), "IOException on record entry. FMH", Toast.LENGTH_LONG);
            	}
            }
        });

        Toast.makeText(getApplicationContext(), "The packageName is " +
                getPackageName(), Toast.LENGTH_LONG).show();
        
        chrono.setOnChronometerTickListener(new OnChronometerTickListener() {
        	public void onChronometerTick(Chronometer arg0) {
        		long then = chrono.getBase();
        		long now = SystemClock.elapsedRealtime();
        		long elapsed = (now-then)/1000;	// Seconds
        		long hours=elapsed/3600;
        		long minutes=(elapsed/60)%60;
        		long seconds=elapsed%60;
        		currentTime=String.format("%02d",hours)+":"+String.format("%02d",minutes)+":"+String.format("%02d",seconds);
        		arg0.setText(currentTime);
        		elapsedTime=now;
        		
        	}
        });

    }

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	theClient = parent.getItemAtPosition(pos).toString();
        	Toast.makeText(parent.getContext(), "The client is " +
        		theClient, Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView<?> parent) {
          theClient="";
        }
    }
}

